package main

import (
	"context"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/registry/consul/v2"
	"log"
	"microdemo/proto"
)

type Service struct {
}

func (s *Service) Register(ctx context.Context, req *proto.RegisterRequest, res *proto.RegisterResponse) error {
	res.Username = req.Username
	res.Token = "13123412"
	res.Nickname = req.Nickname
	return nil
}

func main() {
	reg := consul.NewRegistry(func(options *registry.Options) {
		options.Addrs = []string{"127.0.0.1:8500"}
	})
	// 实例化
	srv := micro.NewService(micro.Name("user"), micro.Address("127.0.0.1:8001"), micro.Registry(reg))
	// 初始化
	srv.Init()
	// 注册服务
	err := proto.RegisterUserHandler(srv.Server(), new(Service))
	if err != nil {
		log.Fatalln("注册服务失败", err)
	}
	// 运行服务
	err = srv.Run()
	if err != nil {
		log.Fatalln("启动服务失败", err)
	}
}
